package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.MemberSetmeal;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestBody;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    //查询所有套餐
    @RequestMapping("/getAllSetmeal")
    public Result getAllSetmeal(){
        try {
            List<Setmeal> list = setmealService.finAll();
            //查询成功
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询套餐失败
            return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    //根据套餐ID查询套餐详细信息（套餐基本信息，套餐对应的检查组信息，检查组对应的检查项信息）
    @RequestMapping("/findById")
    public Result findById(int id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/getAllSetmealByphoneNumber")
    public Result getAllSetmealByphoneNumber(String phoneNumber){
        try {
            List<MemberSetmeal> memberSetmeal = setmealService.getAllSetmealByphoneNumber(phoneNumber);
            System.out.println(memberSetmeal);
            return new Result(true,"获取会员所有套餐成功", memberSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取会员所有套餐失败");
        }
    }

    @RequestMapping("/findResultByOrderDate")
    public Result findResultByOrderDate(String orderDate){
        try {
            Date date = DateUtils.parseString2Date(orderDate);
            MemberSetmeal memberSetmeal = setmealService.findResultByOrderDate(date);

            return new Result(true,"获取会员信息成功", memberSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取会员信息失败");
        }
    }

    //根据memberid查询该会员预约的所有套餐
    @RequestMapping("/getOrderSetmealByMemberId")
    public Result getOrderSetmealByMemberId(@RequestBody Map map) {
        try {
            //获取member会员id
            //int memberId = Integer.parseInt(map.get());
            //远程调用setmealService查询服务
            List<Map<String,Object>> list = setmealService.getOrderSetmealByMemberId(map);
            return new Result(true,"查询会员预约套餐信息成功!",list);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return new Result(false,"查询会员预约套餐信息失败!");
    }

}
